<!--
  * 项目产品报价表
  *
  * @Author:    zeb
  * @Date:      2024-10-29 15:26:19
  * @Copyright  jz
-->
<template>
  <a-modal :title="form.id ? '编辑' : '添加'" width="50%" :open="visibleFlag" @cancel="onClose" :maskClosable="false"
    :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 6 }">

      <a-row>
        <!-- <a-col :span="12">
                        <a-form-item label="项目ID"  name="projectId">
                          <a-input-number style="width: 100%" v-model:value="form.projectId" placeholder="项目ID" />
                        </a-form-item>
                  </a-col> -->
        <a-col :span="12">
          <a-form-item label="产品" name="goodsId">
            <a-select style="width: 70%" v-model:value="form.goodsId" placeholder="产品">
              <a-select-option v-for="item in projectMaterialOptions" :key="item" :value="item.goodsId">{{ item.goodsName
                }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="报价/合同价" name="quote">
            <a-input-number style="width: 100%" v-model:value="form.quote" placeholder="报价/合同价" :min="0" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
import { reactive, ref, nextTick } from 'vue';
import _ from 'lodash';
import { message } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { itemQuotationApi } from '/@/api/business/item-quotation/api';
import { projectMaterialApi } from '/@/api/business/project-material/project-material-api';
import { smartSentry } from '/@/lib/smart-sentry';
import { useRoute } from 'vue-router';
const router = useRoute();
// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);

function show(rowData) {
  getqueryMateriaPage()
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}
const projectMaterialOptions = ref([])
async function getqueryMateriaPage() {
  let queryResult = await projectMaterialApi.queryProjectId(router.query.projectId);
  if (queryResult.code == 0) {
    projectMaterialOptions.value = queryResult.data
  }
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const formDefault = {
  id: undefined,
  projectId: undefined, //项目ID
  goodsId: undefined, //产品ID
  quote: undefined, //报价/合同价
};

let form = reactive({ ...formDefault });

const rules = {
  projectId: [{ required: true, message: '项目ID 必填' }],
  goodsId: [{ required: true, message: '产品ID 必填' }],
  quote: [{ required: true, message: '报价/合同价 必填' }],
};

// 点击确定，验证表单
async function onSubmit() {
  form.projectId = router.query.projectId
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    if (form.id) {
      await itemQuotationApi.update(form);
    } else {
      await itemQuotationApi.add(form);
    }
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

defineExpose({
  show,
});
</script>